package com.bdc.memory.dao.impl;

import com.bdc.BdcVar;
import com.bdc.memory.dao.IMemoryDao;
import com.bdc.memory.domain.MemoryInfo;
import com.google.inject.Inject;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;

/**
 * User: shawn
 * Date: 2011-6-20
 * Time: 20:54:47
 */
public class IMemoryDaoImpl implements IMemoryDao {

    @Inject
    private EntityManager em;

    public void addMemory(MemoryInfo info) {
        em.persist(info);
    }

    public void delMemory(long recordId) {
        Query query = em.createNativeQuery("DELETE FROM memory WHERE recordId = ?");
        query.setParameter(1, recordId);
        query.executeUpdate();
    }

    public List<MemoryInfo> getMemoryList(long recordId) {
        Query query = em.createNativeQuery("SELECT * FROM memory WHERE recordId = ?", MemoryInfo.class);
        query.setParameter(1, recordId);
        return query.getResultList();
    }

    public List<MemoryInfo> getMemoryList(long userId, long time) {
        String sql = "SELECT * FROM memory WHERE userId = ? AND scheduleDate <= ? AND lastScore < ?";
        Query query = em.createNativeQuery(sql, MemoryInfo.class);
        query.setParameter(1, userId);
        query.setParameter(2, time);
        query.setParameter(3, BdcVar.SCORE_REMEMBER_AGAIN);
        return query.getResultList();
    }

    public List<MemoryInfo> getMemoryListOrderBySDay(long recordId) {
        Query query = em.createNativeQuery("SELECT * FROM memory WHERE recordId = ? ORDER BY scheduleDate", MemoryInfo.class);
        query.setParameter(1, recordId);
        return query.getResultList();
    }
}
